/************************************************************************************
* YampConfig.h                                                                      *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "YampConfig.h"

#include <QSqlDatabase>
#include <QVariant>
#include <QDebug>
#include <QSqlError>


YampConfig* YampConfig::__instance = new YampConfig();

YampConfig::YampConfig() {
}


YampConfig* YampConfig::instance() {
    if(__instance == NULL) {
        __instance = new YampConfig();
        __instance->init();
    }
    return __instance;
}


void YampConfig::init() {

}

void YampConfig::addConfig(QString key, QVariant* value) {
    if(value != NULL) {
        m_confMap.insert(key, value);
    }
}


void YampConfig::addConfig(QString key, QString value) {
    m_confMap.insert(key, new QVariant(value));
}


void YampConfig::addConfig(QString key, int value) {
    m_confMap.insert(key, new QVariant(value));
}



void YampConfig::addConfig(QString key, double value) {
    m_confMap.insert(key, new QVariant(value));
}


void YampConfig::removeConfig(QString key) {
    if(m_confMap.contains(key)) {
        delete m_confMap.value(key);
        m_confMap.remove(key);
    }
}


bool YampConfig::hasConfig(QString key) {
    return m_confMap.contains(key);
}


QVariant* YampConfig::getConfig(QString key, QVariant *fallback) {
    if(m_confMap.contains(key)) {
         return m_confMap.value(key);
    }
    return fallback;
}


QString YampConfig::getStringConfig(QString key, QString fallback) {
    if (m_confMap.contains(key)) {
        m_confMap.value(key)->toString();
    }
    return fallback;
}



int YampConfig::getIntConfig(QString key, int fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        int value = m_confMap.value(key)->toInt(&ok);
        return ok ? value : fallback;
    }
    return fallback;
}


double YampConfig::getDoubleConfig(QString key, double fallback) {
    if (m_confMap.contains(key)) {
        bool ok;
        double value = m_confMap.value(key)->toDouble(&ok);
        return ok ? value : fallback;
    }
    return fallback;
}
